<?php

namespace Shared\LocalSyntaxSpecific;

/**
 * 
 * @author Jordan Minchev <j.minchev@object.bg>
 * @copyright (c) 2014, ASPECT.BG Ltd.
 */
class ListDecorator {

    private static $marks = array(
        '?',
        '!',
        ';',
        ':',
        ',',
        '-',
        '–'
    );

    public static function cut($string, $length, $allowedTags = null) {
        $allowedTags = $allowedTags ? : '<i><em><sub><sup><strong><b><u><span>';
        $string = strip_tags($string, $allowedTags);
        
        $string = \Shared\Services\TruncateText::truncate($string, $length);
        $string = preg_replace('/\.([^\.])/', '. $1', $string);
        $string = preg_replace('/(([0-9]+)\. ([0-9]+)((\.) ([0-9]+))?)/', '$2.$3$5$6', $string);
        $string = html_entity_decode($string, null, 'UTF-8');

        $replace = array_map(function($value) {
            return $value . ' ';
        }, self::$marks);
        $string = str_replace(self::$marks, $replace, $string);
        $string = str_replace('text- decoration', 'text-decoration', $string);
        $tidy = new \tidy;
        $tidy->parseString($string, array('clean' => true,'output-xhtml' => true,'show-body-only' => true,'wrap' => 0), 'utf8');
        $string =  $tidy->value;
        return $string;
    }

}
